<?php
/* @var $this EquipeController */
/* @var $model Equipe */
/* @var $form CActiveForm */
?>

<style>

td {
	text-align: center;
}

#bureaux {
	width: 100%;
}

#bureaux td {
	border: 1px solid black;
}

</style>

<div class="form well">

<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id'=>'equipe-form',
	// Please note: When you enable ajax validation, make sure the corresponding
	// controller action is handling ajax validation correctly.
	// There is a call to performAjaxValidation() commented in generated controller code.
	// See class documentation of CActiveForm for details on this.
	'enableAjaxValidation'=>false,
));

// script d'ajout de corps d'états
Yii::app()->clientScript->registerScript('addSpe', 
"function addSpecialite( index ) {
	var nbCE = $('.bureau' + index + ' .corpsEtat > *').length;
	console.log( 'index : ' + index + ' - nbCE : ' + nbCE );
	jQuery.ajax({
			type: 'POST',
			url: '".Yii::app()->baseUrl."/equipe/addSpecialite/index/'+ index + '/nbCE/' + nbCE,
			success: function(html) { 
				$('.bureau' + index + ' .corpsEtat').append(html);
			}
		})
}",
 CClientScript::POS_END);

// script d'ajout des observations des entreprises dans le champ observation equipe
Yii::app()->clientScript->registerScript('addObservationEntreprises', 
	"$('#observationEntreprise').click(function(){
		var ids = $('#Equipe_idArchitecte').val();

		var nbAssocies = $('#architectes tr').length - 2; // 1 ligne pour header + 1 ligne architecte de base
		for ( var i = 0; i < nbAssocies; i++ ) { 
			var idEntreprise = $('#EquipeHasAssocie_'+ i +'_idEntreprise').val();
			ids = ids + ',' + idEntreprise;
		}
		
		jQuery.ajax({
			type: 'POST',
			url: '".Yii::app()->baseUrl."/equipe/ajaxObservationEntreprise/ids/'+ ids,
			success: function(html) { 
				$('#Equipe_observation').val(html);
			}
		});
	});",
 CClientScript::POS_READY);
 /*jQuery.ajax({
			type: 'POST',
			url: '".Yii::app()->baseUrl."/equipe/addAssocie/nbAssocies/'+$('#architectes tr').length,
			success: function(html) { 
				$('#architectes').append(html);
			}
		})*/

// script d'ajout de secteurs d'activités
Yii::app()->clientScript->registerScript('addAssocie', 
	"$('#addAssocies').click(function(){
		jQuery.ajax({
    			type: 'POST',
			url: '".Yii::app()->baseUrl."/equipe/addAssocie/nbAssocies/'+$('#architectes tr').length,
			success: function(html) { 
				$('#architectes').append(html);
			}
		})
	});",
 CClientScript::POS_READY);

// script d'ajout de bureaux d'étude
Yii::app()->clientScript->registerScript('addBE', 
	"$('#addBE').click(function(){
                var nbBE = ($('#bureaux tr').length-1) / 2; // 2 lignes par BE - l'entete
		console.log(nbBE);
		jQuery.ajax({
			type: 'POST',
			url: '".Yii::app()->baseUrl."/equipe/addBE/nbBE/'+nbBE,
			success: function(html) { 
                                $('#bureaux').show();
				$('#bureaux').append(html);
			}
		})
	});",
 CClientScript::POS_READY);
 

?>
    <?php echo $form->errorSummary($model); ?>
	
    <div class="data">
        <legend> <img src="<?php echo Yii::app()->theme->baseUrl;  ?>/images/edit_user.png"> Architectes</legend>

	<table id="architectes" border="1">
		<tr>
			<th style="width:30px"></th>
			<th style="width:200px">Architecte</th>
			<th>Attestation architecte</th>
			<th style="width:120px">Observation</th>
                        <th></th>
		</tr>
		<tr>
			<td></td>
			<td>
			<?php /*echo $form->label($model, 'idArchitecte', array('for' => 'autoCompleteArchitect'));*/ ?>
			<?php 
				$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
					'name' => 'autoCompleteArchitect',
					'value' => isset( $model->idArchitecte )
								? Entreprise::model()->findByPk( $model->idArchitecte )->nom
								: '',
					'source' => $this->createUrl('equipe/autocompleteEntreprise/'),
					'options' => array(
						'select' => "js:function(event, ui) {
							$('#Equipe_idArchitecte').val( ui.item. id );
						}",
						
					),
					'htmlOptions' => array(
						'onblur' => "if ( $('#autoCompleteArchitect').val().length == 0 ) $('#Equipe_idArchitecte').val('');"
					)
				));
			?>
			<?php echo $form->hiddenField($model, 'idArchitecte')?>
			</td>
			<td>
			<?php echo $form->checkBox($model, 'attestationArchitecte') ?>
			</td>
                        <td></td>	
		<td>
			<?php echo CHtml::button('+', array('class' => 'btn', 'id' => 'addAssocies')); ?>
		</td>
		</tr>

		<?php if( !empty($associes) ) : ?>
		
			<?php foreach( $associes as $index => $associe ) : ?>

				<tr id="associe<?php echo $index ?>">
					<td>
						 <?php echo CHtml::ajaxLink(
								'<span class="icon-trash"></span>',
								Yii::app()->createUrl('equipe/deleteAssocie', array('idAssocie' => $associe->idEntreprise, 'idEquipe' => $model->idEquipe)),
								array(
									'type' => 'POST',
									'success' => 'function(){$("#associe'. $index .'").remove();}'
								),
								array(
									'confirm' => 'Êtes vous sûr ?'
								)
							); ?>
					</td>
					<td>
						<?php 
							$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
								'name' => "autoCompleteAssocie$index",
								'value' => isset( $associe->idEntreprise )
											? Entreprise::model()->findByPk( $associe->idEntreprise )->nom
											: '',
								'source' => $this->createUrl('equipe/autocompleteEntreprise/'),
								'options' => array(
								'empty' => "js:function(event, ui) {
									
								}",
								'select' => "js:function(event, ui) {
									$('#EquipeHasAssocie_".$index."_idEntreprise').val( ui.item. id );
								}",
							)));
						?>
						<?php echo $form->hiddenField($associe, "[$index]idEntreprise")?>
					</td>
					<td><?php echo $form->checkBox($associe, "[$index]attestationArchitecte") ?></td>
					<td><?php echo $form->textArea($associe, "[$index]observation") ?></td>
					
				</tr>
			<?php endforeach ?>
			
		<?php endif ?>

		</table> <!--  Architectes -->
            </div>
            <p>&nbsp;</p>
                
            
            <div class="data">
                <legend><img src="<?php echo Yii::app()->theme->baseUrl;  ?>/images/agt_family.png"> Bureaux d'étude</legend>
				
		<div class="data">
			<?php echo CHtml::button('Ajouter un BE', array('class' => 'btn', 'id' => 'addBE', 'style' => 'float: right')); ?>
		</div>		
		
		<?php if( empty($bureaux) ) : ?>
                    <table id="bureaux" class="hide" border=1>
                    <tr>
                            <th style="width:30px"></th>
                            <th style="width:200px">Bureau d'étude</th>
                            <th>DC1</th>
                            <th>DC2</th>
                            <th>Ajouter une spécialité</th>
                    </tr>
                    </table>
                <?php ELSE: ?>
                    <table id="bureaux" border=1>
                    <tr>
                            <th style="width:30px"></th>
                            <th style="width:200px">Bureau d'étude</th>
                            <th>DC1</th>
                            <th>DC2</th>
                            <th>Ajouter une spécialité</th>
                    </tr>
			<?php foreach ( $bureaux as $index => $bureau ) : ?>
			
				<tr class="bureau<?php echo $index ?>" border=1>
			
					<td><?php echo CHtml::ajaxLink(
							'<span class="icon-trash"></span>',
							Yii::app()->createUrl(Yii::app()->baseUrl.'/equipe/deleteBureauEtude', array('idEntreprise' => $bureau->idEntreprise, 'idEquipe' => $model->idEquipe)),
							array(
								'type' => 'POST',
								'success' => 'function(){$(".bureau'. $index .'").remove();}'
							),
							array(
								'confirm' => 'Êtes vous sûr ?'
							)
						); ?>
					</td>
					<td>
						<?php 
							$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
								'name' => "autoCompleteBureauEtude$index",
								'value' => isset( $bureau->idEntreprise )
											? Entreprise::model()->findByPk( $bureau->idEntreprise )->nom
											: '',
								'source' => $this->createUrl(Yii::app()->baseUrl.'/equipe/autocompleteEntreprise/'),
								
                                                                'options' => array(
								'empty' => "js:function(event, ui) {
									
								}",
								'select' => "js:function(event, ui) {
									$('#EquipeHasBureauetude_".$index."_idEntreprise').val( ui.item. id );
								}",
							)
							));
						?>
						<?php echo $form->hiddenField($bureau, "[$index]idEntreprise")?>
					</td>
					
					<td><?php echo $form->checkBox($bureau, "[$index]DC1") ?></td>
					
					<td><?php echo $form->checkBox($bureau, "[$index]DC2") ?></td>
				
					<td><?php echo CHtml::button( 'Spécialité', array('onclick' => 'addSpecialite('.$index.')', 'class' => 'btn') ) ?></td>
					
					<tr class="bureau<?php echo $index ?>">
						<td  class="corpsEtat" colspan="5" style="text-align: left">
							<?php $specialites = BureauetudeHasSpecialite::model()->findAllByAttributes( array('idEquipe' => $model->idEquipe, 'idEntreprise' => $bureau->idEntreprise))?>
							<?php foreach( $specialites as $indexSpecialite => $specialite ) : ?>
							
								<?php echo CActiveForm::dropDownList($specialite, "[$index][$indexSpecialite]idSpecialite",
									CHtml::listData(Specialite::model()->findAll(array('order' => 'libelle')), 'idSpecialite', 'libelle'), array('empty' => '','style'=>'width: auto')); ?>
						
							<?php endforeach ?> <!-- $specialites as $indexSpecialite => $specialite -->
						</td>
					</tr>
			
				</tr>
			
			<?php endforeach ?> <!-- $bureax as $bureau -->
		</table>
		<?php endif ?>
        </div>        
        <p>&nbsp;</p>
	
	
	
      
	<div class="data">
		<legend> <img src="<?php echo Yii::app()->theme->baseUrl;  ?>/images/ark_view.png"> Observation</legend>           
		<?php echo $form->textArea($model,'observation',array('maxlength'=>512,'rows'=>'6', 'cols'=>'120')); ?>
		<?php echo $form->error($model,'observation'); ?>
		<?php echo CHtml::button('Observations entreprise', array('id' => 'observationEntreprise', 'class' => 'btn') ); ?>
	</div>
        
        
        <div class="data">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Créer' : 'Sauvegarder', array('class' => 'btn', 'style' => 'float: right')); ?>
	</div>        
        <p>&nbsp;</p>
        
<?php $this->endWidget(); ?>

</div><!-- form -->
